package com.hawk.eye.server.service.impl;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hawk.eye.common.page.PageQuery;
import com.hawk.eye.common.page.PageResult;
import com.hawk.eye.framework.utils.CollectionUtil;
import com.hawk.eye.framework.utils.ExceptionUtils;
import com.hawk.eye.framework.utils.RegexUtil;
import com.hawk.eye.framework.utils.StreamUtil;
import com.hawk.eye.server.convert.WhiteListConvert;
import com.hawk.eye.server.domain.dto.req.WhiteListReqDTO;
import com.hawk.eye.server.domain.dto.resp.WhiteListRespDTO;
import com.hawk.eye.server.domain.po.WhiteList;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hawk.eye.server.mapper.WhiteListMapper;
import com.hawk.eye.server.service.IWhiteListService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author salad
 * @date 2023-10-04
 */
@Service
@RequiredArgsConstructor
public class WhiteListServiceImpl extends ServiceImpl<WhiteListMapper, WhiteList> implements IWhiteListService {

    private final WhiteListMapper whiteListMapper;

    @Override
    public List<WhiteListRespDTO> getWhiteList(Long serviceId, LocalDateTime now) {
        return WhiteListConvert.INSTANCE.toList(whiteListMapper.getWhiteList(serviceId, now));
    }

    @Override
    public boolean isWhiteList(Long serviceId, Map<String, Object> alarmLogAsMap, LocalDateTime now) {
        List<WhiteListRespDTO> whiteList = getWhiteList(serviceId, now);
        if (CollectionUtil.isEmpty(whiteList)) {
            return false;
        }
        Collection<Object> logValues = alarmLogAsMap.values();
        for (WhiteListRespDTO wl : whiteList) {
            String matchKeyword = wl.getMatchKeyword();
            Pattern compile = RegexUtil.compile(matchKeyword);
            long count;
            if (compile != null) {
                count = StreamUtil
                        .count(logValues.stream(), log -> RegexUtil.matcher(log.toString(), compile));
            } else {
                count = StreamUtil
                        .count(logValues.stream(), log -> {
                            if (log == null) {
                                return false;
                            }
                            return log.toString().contains(matchKeyword);
                        });
            }
            if (count > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public PageResult<WhiteListRespDTO> page(PageQuery pageQuery, WhiteListReqDTO reqDTO) {
        reqDTO.setFailureTimeLine(LocalDateTime.now());
        IPage<WhiteListRespDTO> pages = whiteListMapper.page(pageQuery.toPage(), reqDTO);
        return PageResult.of(pages);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean save(WhiteListReqDTO reqDTO) {
        return save(WhiteListConvert.INSTANCE.to(reqDTO));
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean update(WhiteListReqDTO reqDTO) {
        ExceptionUtils.isEmptyThrow(reqDTO.getId(), "参数{}不允许为空", "id");
        return updateById(WhiteListConvert.INSTANCE.to(reqDTO));
    }

    @Override
    public WhiteListRespDTO detail(Long id) {
        ExceptionUtils.isEmptyThrow(id, "参数{}不允许为空", "id");
        return WhiteListConvert.INSTANCE.to(getById(id));
    }

}
